package cc.avatar.model.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GenericEntityDao<T> extends
		HibernateDaoSupport {

	protected Class<T> entityClass;

	public Class<T> getEntityClass() {
		return this.entityClass;
	}

	public GenericEntityDao() {
	}

	public GenericEntityDao(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	@SuppressWarnings("unchecked")
	public T getById(Long id) {
		return (T) this.getHibernateTemplate().get(this.entityClass, id);
	}

	@SuppressWarnings("unchecked")
	public List<T> getAll() {
		return (List<T>) (this.getHibernateTemplate().loadAll(this.entityClass));
	}

	@SuppressWarnings("unchecked")
	public T load(Long id) {
		return (T) this.getHibernateTemplate().load(this.entityClass, id);
	}

	public void load(T entityObject, Long id) {
		this.getHibernateTemplate().load(entityObject, id);
	}

	public void delete(T entityObject) {
		this.getHibernateTemplate().delete(entityObject);
	}

	public void deleteById(Long id) {
		this.delete(this.getById(id));
	}

	public void refresh(T entityObject) {
		this.getHibernateTemplate().refresh(entityObject);
	}

	public void evict(T entityObject) {
		this.getHibernateTemplate().evict(entityObject);
	}

	public void save(T entityObject) {
		this.getHibernateTemplate().saveOrUpdate(entityObject);
	}

	@SuppressWarnings("unchecked")
	public T merge(T entityObject) {
		return (T) this.getHibernateTemplate().merge(entityObject);
	}

}